package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baidubce.qianfan.util.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.service.IElderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 老人Service业务层处理
 *
 * @author alexis
 * @date 2025-02-25
 */
@Service
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements IElderService {
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询老人
     *
     * @param id 老人主键
     * @return 老人
     */
    @Override
    public Elder selectElderById(Long id) {
        return elderMapper.selectById(id);
    }

    /**
     * 查询老人列表
     *
     * @param elder 老人
     * @return 老人
     */
    @Override
    public List<Elder> selectElderList(Elder elder) {
        return elderMapper.selectElderList(elder);
    }

    /**
     * 新增老人
     *
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int insertElder(Elder elder) {
        return elderMapper.insert(elder);
    }

    /**
     * 修改老人
     *
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int updateElder(Elder elder) {
        return elderMapper.updateById(elder);
    }

    /**
     * 批量删除老人
     *
     * @param ids 需要删除的老人主键
     * @return 结果
     */
    @Override
    public int deleteElderByIds(Long[] ids) {
        return elderMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除老人信息
     *
     * @param id 老人主键
     * @return 结果
     */
    @Override
    public int deleteElderById(Long id) {
        return elderMapper.deleteById(id);
    }

    @Override
    public TableDataInfo<Elder> pageQuery(Integer pageNum, Integer pageSize, Integer status, String name, String idCardNo) {
        LambdaQueryWrapper<Elder> queryWrapper = new LambdaQueryWrapper<>();
        Page<Elder> page = new Page<>(pageNum, pageSize);
        //按照状态查询
        queryWrapper.eq(ObjectUtil.isNotNull(status), Elder::getStatus, status);
        queryWrapper.like(StrUtil.isNotBlank(name), Elder::getName, name);
        queryWrapper.like(StrUtil.isNotBlank(idCardNo), Elder::getIdCardNo, idCardNo);
        //sql返回结果，只展示主键、名字、身份照号、床位编号
        queryWrapper.select(Elder::getId, Elder::getName, Elder::getIdCardNo, Elder::getBedNumber);
        page = page(page, queryWrapper);
        //返回分页结果
        return getDataTable(page);
    }

    /**
     * 封装分页结果
     */
    private TableDataInfo<Elder> getDataTable(Page<Elder> page) {
        //封装分页结果
        TableDataInfo<Elder> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("请求成功");
        tableDataInfo.setRows(page.getRecords());
        return tableDataInfo;
    }
}
